import axios from 'axios';
// 全局组件在这没用，需要再安装一次
import Vue from 'vue';
import {Dialog} from 'vant'
Vue.use(Dialog);
const baseUrl = 'http://localhost:8083'
const version = '/api/v1'


const instance = axios.create({
  baseURL: baseUrl + version,
  timeout: 7000,
  headers:{}
})

// 请求拦截
instance.interceptors.request.use(function(config) {
  config.headers.Authorization = localStorage.getItem('token');
  return config;
}, function(err) {
  return Promise.reject(err)
})

// 响应拦截
instance.interceptors.response.use(function(response) {
  let res = null;
  if(response.status === 200) {
    let data = response.data;
    // console.log(data.err);
    if(data.err === 0) {
      res = data.data;
    }
    // else if(data.err === -1){
    //   res = '添加成功'
    // }
    else {
      Dialog.alert({
        title: '错误',
        message: data.msg
      })
    }
  }else {
    Dialog.alert({
      title: '错误',
      message: '网络异常',
    });
  }
  return res;
}, function(err) {
  return Promise.reject(err);
})
export default instance;